package testview.zhen.com.myapplication.design.state_design

/**
 * @author :create by ldr
 * time:2022/10/25 16:22
 * @descript:在状态模式（State Pattern）中，类的行为是基于它的状态改变的。这种类型的设计模式属于行为型模式。

在状态模式中，我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象。
 */
open interface State {
    fun doAction(context: Context?)
}

class StartState : State {
    override fun doAction(context: Context?) {
        println("Player is in start state")
        context!!.state = this
    }

    override fun toString(): String {
        return "Start State"
    }
}

class StopState : State {
    override fun doAction(context: Context?) {
        println("Player is in stop state")
        context!!.state = this
    }

    override fun toString(): String {
        return "Stop State"
    }
}

class Context {
    var state: State? = null
}

fun main() {
    val context = Context()
    val startState = StartState()
    startState.doAction(context)
    println(context.state.toString())
    val stopState = StopState()
    stopState.doAction(context)
    println(context.state.toString())
}
